// 获取验证码
var verify_code_lock = 0
$('#sync_verify_code').click(function () {
  if (verify_code_lock > 0) {
    return
  }

  let regex = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
  if (regex.test($('#register-email').val()) === false) {
    layer.msg('请输入正确的邮箱地址')
    return
  }

  const code = parseInt(Math.random() * 1000000)
  sessionStorage.setItem('code', code)

  axios.get('/auth/verify_code/', {
    params: {
      'email': $('#register-email').val(),
      'verify_code': code
    },
    headers: { 'X-CSRFToken': CsrfToken() }
  }).then(respone => {
    if (respone.data[0].code === 500) {
      layer.msg('邮箱格式不正确,请重新填写')
      shutdown()
    }
    layer.msg('邮件发送成功')
    verify_code_lock++
  })

  maxtime = 60
  timer = setInterval('CountDown()', 1000)
  $('#sync_verify_code').hide()
  $(this).text('重新获取')
  verify_code_lock = 0
})

//计时器
var maxtime = 0

function CountDown () {
  if (maxtime > 0) {
    seconds = Math.floor(maxtime % 60)
    msg = seconds + '秒后可重新获取'
    $('#timer').html(msg)
    --maxtime
  } else {
    shutdown()
  }
}

function shutdown () {
  maxtime = 0
  $('#sync_verify_code').show()
  $('#timer').html('')
  clearInterval(timer)
}

layui.use(['form', 'element'], function () {
  var form = layui.form

  form.verify({
    username: function (value) {
      console.log(value)
      let regex = /^[a-zA-Z0-9_-]{5,16}$/
      let result = regex.test(value)
      if (result === false || value.length < 5) {
        return '用户名5-16个字符,只包含数字、字母、下划线'
      }
    }
    , pass: [
      /^[\S]{6,12}$/
      , '密码必须6到12位，且不能出现空格'
    ]
    , content: function (value) {
      layedit.sync(editIndex)
    },
    email: function (value) {
      let regex = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/
      if (regex.test(value) === false) {
        return '请输入正确的邮箱地址'
      }
    },
    verify: function (value) {
      let regex = /^[\S]{6}$/
      if (regex.test(value) === false) {
        return '请获取并填写验证码,验证码必须为6位数字'
      }
    }
  })

//登陆
  form.on('submit(login)', function (data) {
    console.log(data.field)
    axios.post('/auth/login/', data.field, {
      headers: {
        'X-CSRFToken': CsrfToken()
      }
    }).then(function (res) {
      $('#message').html(res.data.message)
      if (res.data.login_code === 302) {
        window.history.back()
      }
    }).catch(function (error) {
      console.log(error)
    })
    return false
  })

//注册
  form.on('submit(register)', function (data) {
    console.log(data.field)
    if (data.field.verify_code !== sessionStorage.getItem('code')) {
      layer.msg('验证码错误')
      return false
    } else {
      sessionStorage.setItem('code', '')
    }
    axios.post('/auth/register/', data.field, {
      headers: {
        'X-CSRFToken': CsrfToken()
      }
    }).then(
      function (res, status) {
        if (res.data.code === 500) {
          console.log('注册失败', data)
          layer.msg(res.data.message)
        } else if (res.data.code === 200) {
          console.log('注册成功', data)
          setTimeout(function () {
            window.location = '/'
          }, 500)
        }
      }
    )
    return false
  })

  var element = layui.element
  var layid = location.hash.replace(/^#logTab=/, '')
  element.tabChange('logTab', layid)
  element.on('tab(logTab)', function () {
    location.hash = 'logTab=' + this.getAttribute('lay-id')
  })
})

